package com.itlong.cloud.controller.old;

import com.itlong.cloud.POJO.DTO.smartcard.SmartCardOldGetDeviceRecordDTO;
import com.itlong.cloud.POJO.VO.smartcard.SmartCardOldEquipRecordVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.equip.smartcard.ISmartCardOldEquipRecordServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 * DEMO示例。
 * <desc/>
 *
 * @createDate 2017-12-19.
 */
@RestController
@RequestMapping("/device/deviceRecord")
public class SmartCardOldEquipRecordController {

    private static final Logger LOG = LoggerFactory.getLogger(SmartCardOldEquipRecordController.class);

    //获取通行记录接口
    @Autowired
    ISmartCardOldEquipRecordServiceClient iSmartCardOldEquipRecordServiceClient;
    /**
     * <desc>
     *      获取设备通行记录。
     * <desc/>
     *
     * @param pageDTO
     * @return
     * @author shengen.T
     * @createDate 2017-12-18
     */
    @LoggerInfo(operateDesc = "旧-获取设备通行记录")
    @ResponseBody
    @RequestMapping(path = "/manage/list",method = RequestMethod.POST)
    public Message getEquipRecord(SmartCardOldGetDeviceRecordDTO pageDTO){
        Message message;
        try {
            //判断用户名
            if(!PlatformConstants.OLD_SMARTCARD_LOGIN_USER.equals(pageDTO.getMyUserName())){
                return new Message(Code.LOGIN_ERROR);
            }
            //判断项目编号
            if (StringUtils.isBlank(pageDTO.getProjectId()) || !pageDTO.getProjectId().matches("[0-9]{8}")) {
                LOG.info("【旧_一卡通】获取通行记录列表_项目编号非法】" + pageDTO.getProjectId());
                return new Message(Code.SDK_PROJECTID_ERR,"项目编号不合法");
            }
            //判断设备类型
            if((!"1".equals(pageDTO.getDeviceType()))&&(!"2".equals(pageDTO.getDeviceType()))){
                return new Message(Code.PARAM_ERR);
            }
            //判断是否需要最新记录
            if((!"1".equals(pageDTO.getLatestRecord()))&&(!"0".equals(pageDTO.getLatestRecord()))){
                return new Message(Code.PARAM_ERR);
            }
            //判断每页显示数量,如果-1则不分页，默认10
            if("-1".equals(pageDTO.getPageSize())){
                pageDTO.setPageSize("10");
            }
            Page<SmartCardOldEquipRecordVO> page = iSmartCardOldEquipRecordServiceClient.getEquipRecordPage(pageDTO);
            Map<String,Object> params = new HashMap<>();
            params.put("totalNumber",page.getPage().getTotalNumber());
            params.put("deviceRecords",page.getRows());
            message = new Message(Code.SUCCESS,params);
        }catch (Exception e){
            throw new DataAccessException("【旧一卡通】获取通行记录信息失败",e);
        }
        return message;
    }
}
